﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 插入图形
{
    public partial class Form1 : Form
    {
        bool insertFlag = false;
        Point myDown = Point.Empty;
        List<Point> rectPoints = new List<Point>();//插入矩形坐标点

        public Form1()
        {
            InitializeComponent();
            DoubleBuffered = true;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (myDown == Point.Empty || !insertFlag) return;
            foreach (var points in rectPoints)
            { 
                e.Graphics.DrawRectangle(Pens.Red, new Rectangle(points.X, points.Y, 200, 100));
            }
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (insertFlag)
            {
                //只有在插入的时候 进行坐标的更新和幕布的刷新
                myDown = e.Location;
                Invalidate();//执行paint函数
                rectPoints.Add(myDown);
            }  
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            insertFlag = true;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            insertFlag = false;//插入完成 允许插入标志更改为false
        }
    }
}
